home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / exampleCode / opengl / GLUT / progs / examples / fontdemo.c < prev    next >
C/C++ Source or Header  |  1996-11-11  |  3KB  |  103 lines

  1.  
  2. /* Copyright (c) Mark J. Kilgard, 1994. */
  3.  
  4. /* This program is freely distributable without licensing fees 
  5.    and is provided without guarantee or warrantee expressed or 
  6.    implied. This program is -not- in the public domain. */
  7.  
  8. #include <string.h>
  9. #include <stdio.h>
  10. #include <stdarg.h>
  11. #include <GL/glut.h>
  12.  
  13. void
  14. bitmap_output(int x, int y, char *string, void *font)
  15. {
  16.   int len, i;
  17.  
  18.   glRasterPos2f(x, y);
  19.   len = (int) strlen(string);
  20.   for (i = 0; i < len; i++) {
  21.     glutBitmapCharacter(font, string[i]);
  22.   }
  23. }
  24.  
  25. void
  26. stroke_output(GLfloat x, GLfloat y, char *format,...)
  27. {
  28.   va_list args;
  29.   char buffer[200], *p;
  30.  
  31.   va_start(args, format);
  32.   vsprintf(buffer, format, args);
  33.   va_end(args);
  34.   glPushMatrix();
  35.   glTranslatef(x, y, 0);
  36.   glScalef(0.005, 0.005, 0.005);
  37.   for (p = buffer; *p; p++)
  38.     glutStrokeCharacter(GLUT_STROKE_ROMAN, *p);
  39.   glPopMatrix();
  40. }
  41.  
  42. void
  43. display(void)
  44. {
  45.   glClear(GL_COLOR_BUFFER_BIT);
  46.   bitmap_output(40, 35, "This is written in a GLUT bitmap font.",
  47.     GLUT_BITMAP_TIMES_ROMAN_24);
  48.   bitmap_output(30, 210, "More bitmap text is a fixed 9 by 15 font.",
  49.     GLUT_BITMAP_9_BY_15);
  50.   bitmap_output(70, 240, "                Helvetica is yet another bitmap font.",
  51.     GLUT_BITMAP_HELVETICA_18);
  52.   glMatrixMode(GL_PROJECTION);
  53.   glPushMatrix();
  54.   glLoadIdentity();
  55.   gluPerspective(40.0, 1.0, 0.1, 20.0);
  56.   glMatrixMode(GL_MODELVIEW);
  57.   glPushMatrix();
  58.   gluLookAt(0.0, 0.0, 4.0,  /* eye is at (0,0,30) */
  59.     0.0, 0.0, 0.0,      /* center is at (0,0,0) */
  60.     0.0, 1.0, 0.);      /* up is in postivie Y direction */
  61.   glPushMatrix();
  62.   glTranslatef(0, 0, -4);
  63.   glRotatef(50, 0, 1, 0);
  64.   stroke_output(-2.5, 1.1, "  This is written in a");
  65.   stroke_output(-2.5, 0, " GLUT stroke font.");
  66.   stroke_output(-2.5, -1.1, "using 3D perspective.");
  67.   glPopMatrix();
  68.   glMatrixMode(GL_MODELVIEW);
  69.   glPopMatrix();
  70.   glMatrixMode(GL_PROJECTION);
  71.   glPopMatrix();
  72.   glMatrixMode(GL_MODELVIEW);
  73.   glFlush();
  74. }
  75.  
  76. void
  77. reshape(int w, int h)
  78. {
  79.   glViewport(0, 0, w, h);
  80.   glMatrixMode(GL_PROJECTION);
  81.   glLoadIdentity();
  82.   gluOrtho2D(0, w, 0, h);
  83.   glScalef(1, -1, 1);
  84.   glTranslatef(0, -h, 0);
  85.   glMatrixMode(GL_MODELVIEW);
  86. }
  87.  
  88. int
  89. main(int argc, char **argv)
  90. {
  91.   glutInit(&argc, argv);
  92.   glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
  93.   glutInitWindowSize(465, 250);
  94.   glutCreateWindow("GLUT bitmap & stroke font example");
  95.   glClearColor(1.0, 1.0, 1.0, 1.0);
  96.   glColor3f(0, 0, 0);
  97.   glLineWidth(3.0);
  98.   glutDisplayFunc(display);
  99.   glutReshapeFunc(reshape);
  100.   glutMainLoop();
  101.   return 0;             /* ANSI C requires main to return int. */
  102. }
  103.